home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48_2 / vsrc.tar / voyager7_src / find.c < prev    next >
C/C++ Source or Header  |  1991-02-27  |  3KB  |  181 lines

  1. /*
  2. // Abstract:
  3. //    FIND---Searching for Comment
  4. //
  5. //    The Searhing for Comment module supports searching portions of
  6. //    The comment files for the specified (partial) comment.
  7. //
  8. // Author:
  9. //    Derek S. Nickel
  10. //
  11. // Creation date:
  12. //    16 January 1991
  13. //
  14. // History:
  15. // V01-001    Derek S. Nickel        16-JAN-1991
  16. //    Original.
  17. //
  18. */
  19.  
  20. #include <ctype.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <io.h>
  24.  
  25. #include "find.h"
  26. #include "index.h"
  27. #include "memory.h"
  28. #include "pager.h"
  29. #include "tree.h"
  30.  
  31. #define MAX_LINE 256
  32.  
  33. typedef struct _finddata_t finddata_t;
  34.  
  35. struct _finddata_t {
  36.     long matches;
  37.     bin5_t offset;
  38.     bin5_t start;
  39.     bin5_t end;
  40.     char *data;
  41.     TextFile *tf;
  42.     char cbuf[MAX_LINE];
  43.     char dbuf[MAX_LINE];
  44. };
  45.  
  46. static void search_port(int port_no, bin5_t start, bin5_t end,
  47. long *matches, char *rest);
  48.  
  49. static int write_one_line(node_t *node, finddata_t *db);
  50.  
  51. /***********************************************************************
  52.     Messages.
  53. ***********************************************************************/
  54.  
  55. #define vgr__nomatches \
  56. "%%VOYAGER-I-NOMATCHES, no matches found\n"
  57.  
  58. #define vgr__matched \
  59. "%%VOYAGER-I-MATCHED, found %ld matches\n"
  60.  
  61. /***********************************************************************
  62.     find_comment
  63. ***********************************************************************/
  64.  
  65. void find_comment(bin5_t start, bin5_t end, char *data)
  66. {
  67.     long matches = 0;
  68.  
  69.     /*
  70.     // Search each port in turn.
  71.     */
  72.  
  73.     strupr(data);
  74.  
  75.     if (ports[0].loaded) search_port(0, start, end, &matches, data);
  76.     if (ports[1].loaded) search_port(1, start, end, &matches, data);
  77.     if (ports[2].loaded) search_port(2, start, end, &matches, data);
  78.  
  79.     /*
  80.     // Print summary.
  81.     */
  82.  
  83.     pager(0);
  84.     if (matches == 0)
  85.         printf(vgr__nomatches);
  86.     else
  87.         printf(vgr__matched, matches);
  88. }
  89.  
  90. /***********************************************************************
  91.     search_port
  92. ***********************************************************************/
  93.  
  94. static void search_port(int port_no, bin5_t start, bin5_t end,
  95. long *matches, char *data)
  96. {
  97.     bin5_t min_adr;
  98.     bin5_t max_adr;
  99.     finddata_t db;
  100.     cond_value sys_status;
  101.  
  102.     /*
  103.     // Define allowable search range for this port.
  104.     */
  105.  
  106.     min_adr = ports[port_no].min_adr;
  107.     max_adr = ports[port_no].max_adr;
  108.  
  109.     /*
  110.     // Verify correct sequencing.
  111.     */
  112.  
  113.     if (start > end) {
  114.         bin5_t tmp;
  115.         tmp = start;
  116.         start = end;
  117.         end = tmp;
  118.     }
  119.  
  120.     /*
  121.     // Search this port?
  122.     */
  123.  
  124.     if ((start < min_adr && end < min_adr) ||
  125.         (start > max_adr && end > max_adr))
  126.         return;
  127.  
  128.     /*
  129.     // Force start amd end into range.
  130.     */
  131.  
  132.     if (start < min_adr) start = min_adr;
  133.     if (end > max_adr) end = max_adr;
  134.  
  135.     /*
  136.     // Search for comment.
  137.     */
  138.  
  139.     db.matches = 0;
  140.     db.offset = min_adr;
  141.     db.start = start;
  142.     db.end = end;
  143.     db.tf = ports[port_no].ca_file;
  144.     db.data = data;
  145.  
  146.     /*
  147.     // Search for comments.
  148.     */
  149.  
  150.     sys_status = traverse_tree(
  151.         &db.tf->root,
  152.         write_one_line,
  153.         &db );
  154.  
  155.     *matches += db.matches;
  156. }
  157.  
  158. static int write_one_line(node_t *node, finddata_t *db)
  159. {
  160.     char *cbufp = db->cbuf;
  161.     char *dbufp = db->dbuf;
  162.     bin5_t adr = node->key + db->offset;
  163.  
  164.     if (db->start <= adr && adr <= db->end) {
  165.         if (!node->deleted) {
  166.             fseek(db->tf->f, node->value, SEEK_SET);
  167.             fgets(cbufp, MAX_LINE, db->tf->f);
  168.             db->cbuf[strlen(cbufp)-1] = '\0';
  169.             cbufp += 6;
  170.             strupr(strcpy(dbufp,cbufp));
  171.             if (strstr(dbufp,db->data)) {
  172.                 db->matches++;
  173.                 pager(0);
  174.                 printf("%05lX %s\n", adr, cbufp);
  175.             }
  176.         }
  177.     }
  178.  
  179.     return 1;
  180. }
  181.